package datastructure.tree;

/**
 * @author XY
 * @version 1.0
 * @date 2022/1/12 9:22
 * @Description
 */
public class Leet1335 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    static class Solution {
        public TreeNode sortedArrayToBST(int[] nums) {
            return helper(nums, 0, nums.length - 1);
        }

        private TreeNode helper(int[] nums,int left ,int right){
            if(left>right){
                return null;
            }
            int mid = (left+right)/2;
            TreeNode root = new TreeNode(nums[mid]);
            root.left = helper(nums,left,mid -1 );
            root.right = helper (nums,mid+1, right);
            return root;
        }


    }


    public static void main(String[] args) {
        int[] arr = {-10,-3,0,5,9};
        Leet1335.Solution solution = new Leet1335.Solution();
        solution.sortedArrayToBST(arr);
    }
}
